/*
 * Copyright TLR Team, Inc. All Rights Reserved.
 */
package com.tlr.device.rest;

import com.sun.jersey.spi.container.servlet.ServletContainer;
import com.sun.jersey.spi.container.servlet.WebConfig;
import com.tfb.service.utility.AppConst;
import com.tfb.service.utility.ReminderHourlyJob;
import com.tfb.service.utility.Utils;
import java.util.Timer;
import java.util.TimerTask;
import javax.servlet.ServletException;
import org.apache.log4j.Logger;
import org.quartz.JobExecutionException;
import org.quartz.SchedulerException;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class ApplicationServiceContainer extends ServletContainer {

    private static final Logger log = Logger.getLogger(ApplicationServiceContainer.class);
    private static final long serialVersionUID = 1L;
    private static Timer timer;

    public static class EbanDeviceTimerTask extends TimerTask {

        @Override
        public void run() {
            ReminderHourlyJob job = new ReminderHourlyJob();
            try {
                job.execute(null);
            } catch (JobExecutionException ex) {
            }
        }

    }

    @Override
    public void destroy() {
        super.destroy();
        cancelJob();
    }

    @Override
    public void reload() {
        super.reload();
        cancelJob();
    }

    @Override
    protected void init(WebConfig webConfig) throws ServletException {
        super.init(webConfig);
        try {
            // Handle httpProxy
            String httpProxy = webConfig.getInitParameter("httpProxy");
            String proxyPort = "80";
            if (httpProxy != null && !httpProxy.isEmpty()) {
                String[] proxy = httpProxy.split(":");
                if (proxy.length > 0) {
                    httpProxy = proxy[0].trim();
                }
                if (proxy.length > 1) {
                    proxyPort = proxy[1].trim();
                }
                AppContext.setHttpProxy(httpProxy, Integer.parseInt(proxyPort));
                AppContext.setHttpProxyEnable(true);
            } else {
                AppContext.setHttpProxyEnable(false);
            }
            // Air measure device company token
            String airMeasureDeviceToken = webConfig.getInitParameter(AppConst.AIR_MEASURE_DEVICE_COMPANY_TOKEN);
            AppContext.setAirMeasureDeviceToken(airMeasureDeviceToken);
            String locationCardGroupID = webConfig.getInitParameter(AppConst.LOCATION_CARD_GROUP_ID);
            AppContext.setLocationCardGroupID(locationCardGroupID);
            String hikeWebsocketAddress = webConfig.getInitParameter(AppConst.HIKE_WEB_SOCKET_ADDRESS);
            AppContext.setHikeWebsocketAddress(hikeWebsocketAddress);
            String ebanWebsocketAddress = webConfig.getInitParameter(AppConst.EBAN_WEB_SOCKET_ADDRESS);
            AppContext.setEbanSocketAddress(ebanWebsocketAddress);
            String hikeRest = webConfig.getInitParameter(AppConst.HIKE_SERVER_REST);
            AppContext.setHikeServerRest(hikeRest);
            String notifyaddress = webConfig.getInitParameter(AppConst.PEER_SERVER_ADDRESS);
            AppContext.setPeerNotifyAddress(notifyaddress);
            String strSmsServer = webConfig.getInitParameter(AppConst.SMS_SERVER_ADDRESS);
            AppContext.setSmsServerAddress(strSmsServer);
            String strSmsUser = webConfig.getInitParameter(AppConst.SMS_USER);
            AppContext.setSmsUser(strSmsUser);
            String strSmsPwd = webConfig.getInitParameter(AppConst.SMS_PASSWORD);
            AppContext.setSmsPwd(strSmsPwd);
            String strRegTemp = webConfig.getInitParameter(AppConst.SMS_REGISTER_TEMPLATE);
            AppContext.setSmsRegisterTemplate(strRegTemp);
            String strDocDownloadDir = webConfig.getInitParameter(AppConst.DOC_DOWNLOAD_DIR);
            AppContext.setDocDownloadDir(strDocDownloadDir);
            String filepath = webConfig.getInitParameter(AppConst.FILE_UPLOAD_PATH);
            AppContext.setFileUploadPath(filepath);
            String filesyncaddress = webConfig.getInitParameter(AppConst.FILE_SYNC_ADDRESS);
            AppContext.setPeerSyncAddress(filesyncaddress);
            AppContext.setBeanContext(new ClassPathXmlApplicationContext(new String[]{"classpath:spring.xml", "classpath:spring-mybatis.xml"}));
            buildJob();
        } catch (Exception ex) {
            log.error(Utils.getFullException(ex));
        }
    }

    public synchronized static void buildJob() throws SchedulerException {
        if (timer == null) {
            timer = new Timer();
            timer.scheduleAtFixedRate(new EbanDeviceTimerTask(), 0, Math.round(60 * 60 * 1000));
        }
    }

    public synchronized static void cancelJob() {
        if (timer != null) {
            timer.cancel();
            timer = null;
        }
    }
}
